package com.changgou.goods.controller;

import com.changgou.framework.entity.PageResult;
import com.changgou.framework.entity.Result;
import com.changgou.framework.entity.StatusCode;
import com.changgou.goods.service.SpecService;
import com.haha.goods.entity.Spec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Yang
 * @date 2020/11/7
 */
@RestController
@RequestMapping("/spec")
@CrossOrigin
public class SpecController {
    @Autowired
    private SpecService specService;

    /***
     * Spec分页条件搜索实现
     * @param spec
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestBody(required = false) Spec spec, @PathVariable int page, @PathVariable  int size){
        //执行搜索
        PageResult pageInfo = specService.findPage(spec, page, size);
        return Result.ok(pageInfo);
    }

    /***
     * Spec分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@PathVariable  int page, @PathVariable  int size){
        //分页查询
        PageResult pageInfo = specService.findPage(null,page, size);
        return Result.ok(pageInfo);
    }
    /***
     * 多条件搜索品牌数据
     * @param spec
     * @return
     */
    @PostMapping(value = "/search" )
    public Result findList(@RequestBody(required = false)  Spec spec){
        PageResult pageResult = specService.findPage(spec,1,Integer.MAX_VALUE);
        return Result.ok(pageResult.getRows());
    }
    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        boolean b = specService.removeById(id);
        return b?Result.ok(null):Result.error("删除失败");
    }

    /***
     * 修改Spec数据
     * @param spec
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody  Spec spec,@PathVariable Integer id){
        //设置主键值
        spec.setId(id);
        //修改数据
        boolean b = specService.updateById(spec);
        return b?Result.ok(null):Result.error("更新失败");
    }

    /***
     * 新增Spec数据
     * @param spec
     * @return
     */
    @PostMapping
    public Result add(@RequestBody   Spec spec){
        boolean b = specService.save(spec);
        return b?Result.ok(null):Result.error("保存失败");
    }

    /***
     * 根据ID查询Spec数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        //根据ID查询
        Spec spec = specService.getById(id);
        return Result.ok(spec);
    }

    /***
     * 查询Spec全部数据
     * @return
     */
    @GetMapping
    public Result findAll(){
        List<Spec> list = specService.list();
        return Result.ok(list);
    }
    /***
     * 根据分类ID查询对应的规格列表
     */
    @GetMapping(value = "/category/{id}")
    public Result findByCategoryId(@PathVariable(value = "id")Integer categoryid){
        //调用Service查询
        List<Spec> specs = specService.findByCategoryId(categoryid);
        return Result.ok(specs);
    }


}
